<template>
  <div class="school-registration">
    <!-- 页面头部 -->
    <header class="header">
      <div class="container">
        <div class="header-content">
          <h1 class="title">中小学仿真系统平台</h1>
          <p class="subtitle">注册账户，共创教育新未来</p>
        </div>
      </div>
    </header>

    <!-- 主要内容区 -->
    <main class="main-content">
      <div class="container">
        <!-- 注册表单卡片 -->
        <div class="registration-card">
          <h2 class="card-title">学校信息申请</h2>
          <p class="card-subtitle">请填写以下信息完成申请</p>

          <!-- 注册表单 -->
          <el-form
              ref="formRef"
              :model="formData"
              :rules="formRules"
              class="registration-form"
              @submit.prevent="handleSubmit"
          >
            <!-- 学校名称 -->
            <el-form-item prop="schoolName">
              <el-input
                  v-model="formData.schoolName"
                  placeholder="请输入学校名称"
                  prefix-icon="SchoolIcon"
              />
            </el-form-item>

            <!-- 学校地址 -->
            <el-form-item prop="address">
              <el-input
                  v-model="formData.address"
                  placeholder="请输入学校详细地址"
                  prefix-icon="MapPinIcon"
              />
            </el-form-item>

            <!-- 联系人姓名 -->
            <el-form-item prop="contactPerson">
              <el-input
                  v-model="formData.contactPerson"
                  placeholder="请输入联系人姓名"
                  prefix-icon="UserIcon"
              />
            </el-form-item>

            <!-- 联系电话 -->
            <el-form-item prop="contactPhone">
              <el-input
                  v-model="formData.contactPhone"
                  placeholder="请输入联系电话"
                  prefix-icon="PhoneIcon"
              />
            </el-form-item>

            <!-- 邮箱 -->
            <el-form-item prop="email">
              <el-input
                  v-model="formData.email"
                  placeholder="请输入邮箱地址"
                  prefix-icon="MailIcon"
              />
            </el-form-item>


            <el-date-picker
                v-model="formData.joinDate"
                type="date"
                placeholder="请选择日期"
            />

            <!-- 同意协议 -->
            <el-form-item prop="agreement">
              <el-checkbox v-model="formData.agreement">
                我已阅读并同意<a href="#" class="link">《申请协议》</a>和<a href="#" class="link">《隐私政策》</a>
              </el-checkbox>
            </el-form-item>

            <!-- 提交按钮 -->
            <el-form-item>
              <el-button
                  :loading="submitting"
                  type="primary"
                  class="submit-button"
                  @click="handleSubmit"
              >
                {{ submitting ? '提交中...' : '提交申请' }}
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                  :loading="submitting"
                  type="primary"
                  style="width:30%;margin: -85px 0 0 180px;font-size: 13px;"
                  class="submit-button"
                  @click="fanhui"
              >
               返回登陆
              </el-button>
            </el-form-item>

          </el-form>
        </div>
      </div>
    </main>

    <!-- 页脚 -->
    <footer class="footer">
      <div class="container">
        <p>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</p>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import axios from "axios";
import {ElMessage} from "element-plus";

// 初始化路由
const router = useRouter();

// 表单数据
const formData = reactive({
  address:'',         //学校地址
  schoolName: '',     //学校名称
  schoolAddress: '',
  contactPerson: '',  //联系人
  contactPhone: '',   //联系电话
  email: '',         //邮箱
  verificationCode: '',
  joinDate:'',        //日期
  agreement: false
});

// 表单引用
const formRef = ref(null);

// 表单验证规则
const formRules = reactive({
  schoolName: [
    { required: true, message: '请输入学校名称', trigger: 'blur' },
    { min: 2, message: '学校名称至少2个字符', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入学校地址', trigger: 'blur' },
    { min: 5, message: '地址信息不完整', trigger: 'blur' }
  ],
  contactPerson: [
    { required: true, message: '请输入联系人姓名', trigger: 'blur' },
    { min: 2, message: '姓名至少2个字符', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  // verificationCode: [
  //   { required: true, message: '请输入验证码', trigger: 'blur' },
  //   { min: 6, max: 6, message: '验证码为6位数字', trigger: 'blur' }
  // ],
  agreement: [
    { required: true, message: '请阅读并同意协议', trigger: 'change' }
  ]
});

// 状态管理
const submitting = ref(false);


const handleSubmit = async () => {
  try {
    const valid = await formRef.value.validate()
    if (!valid) return

    submitting.value = true
    const response = await axios.post('http://localhost:9302/schools', formData) // 注意去掉了 schools 包装

    if (response.data.code === 200) {
      ElMessage.success({
        message: '提交成功！我们将尽快与您联系。',
        duration: 3000
      })
      // 可选：重置表单
      formRef.value.resetFields()
    }
  } catch (error) {
    ElMessage.error({
      message: error.response?.data?.message || '提交失败，请稍后重试',
      duration: 3000
    })
  } finally {
    submitting.value = false
  }
}

function fanhui() {
  router.push('/login')
}
</script>

<style scoped lang="scss">
.school-registration-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.header {
  padding: 40px 0;
  background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
  color: white;
  text-align: center;

  .title {
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 10px;
  }

  .subtitle {
    font-size: 16px;
    opacity: 0.8;
  }
}

.main-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 0;

  .registration-card {
    width: 100%;
    max-width: 500px;
    padding: 40px 30px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
    }

    .card-title {
      font-size: 24px;
      font-weight: bold;
      text-align: center;
      margin-bottom: 10px;
      color: #333;
    }

    .card-subtitle {
      font-size: 14px;
      text-align: center;
      margin-bottom: 30px;
      color: #666;
    }
  }

  .registration-form {
    .el-form-item {
      margin-bottom: 20px;
    }

    .el-input {
      height: 46px;
      border-radius: 8px;

      .el-input__prefix {
        margin-right: 5px;
      }

      .el-input__inner {
        height: 46px;
        border-radius: 8px;
        border-color: #e0e0e0;
        font-size: 14px;

        &:focus {
          border-color: #409eff;
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
        }
      }
    }

    .verification-code-group {
      display: flex;
      gap: 10px;
    }

    .submit-button {
      height: 48px;
      font-size: 16px;
      border-radius: 8px;
      background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
      border: none;

      &:hover {
        background: linear-gradient(135deg, #3a8ee6 0%, #5da9ff 100%);
      }
    }
  }
}

.footer {
  padding: 20px 0;
  text-align: center;
  color: #666;
  font-size: 14px;
  background: white;
  border-top: 1px solid #f0f0f0;
}

.link {
  color: #409eff;
  text-decoration: none;

  &:hover {
    text-decoration: underline;
  }
}

// 适配小屏幕
@media (max-width: 600px) {
  .header {
    padding: 30px 0;
  }

  .main-content {
    padding: 20px;
  }

  .registration-card {
    padding: 30px 20px;
  }
}
</style>
